Class.create("Bitmap", {
	palette: null,
	pixels: null,
	parent: null,
	__construct: function(sprite)
	{
		this.parent = sprite;
		this.palette = [];
		this.pixels = new Array(sprite.width() * sprite.height());
		/*
		var width = sprite.width();
		var height = sprite.height();
		var imageData = this.getPixels(0, 0, width, height);
		var pixels = imageData.data;
		for(var x = 0; x < width; ++x)
		{
			for(var y = 0; y < height; ++y)
			{
				var idx = (x + y * width) * 4;
				var r = pixels[idx + 0];
				var g = pixels[idx + 1];
				var b = pixels[idx + 2];
				var a = pixels[idx + 3];
				var gray = (r + g + b) / 3;
				pixels[idx + 0] = gray;
				pixels[idx + 1] = gray;
				pixels[idx + 2] = gray;
				pixels[idx + 3] = a;
			}
		}
		imageData.data = pixels;
		this.setPixels(imageData, 0, 0);*/
	},
	getPixels: function(x, y, w, h)
	{
		return this.parent.context.getImageData(x, y, w, h);
	},
	setPixels: function(imgData, x, y)
	{
		this.parent.context.putImageData(imgData, x, y);
		//this.parent.invokeArea(x, y, imageData.width, imageData.height);
	}
});

Class.create("Sprite", {
	bitmap: null,
	canvas: null,
	context: null,
	onevent: null,
	__static:
	{
		cache: [],
		fromFile: function(filePath)
		{
			var spr = new Sprite();
			if(spr.context)
			{
				var img = new Image();
				$(img).load(function()
				{
					spr.context.canvas.width = img.width;
					spr.context.canvas.height = img.height;
					spr.context.drawImage(img, 0, 0);
					spr.bitmap = new Bitmap(spr);
					
					if(spr.onevent)
						spr.onevent({data: spr});
				}).error(function()
				{
				}).attr('src', filePath);
			}
			return spr;
		},
		fromSize: function(width, height)
		{
			var spr = new Sprite();
			if(spr.context)
			{
				spr.context.canvas.width = width;
				spr.context.canvas.height = height;
			}
			return spr;
		}
	},
	__construct: function()
	{
		this.canvas = $('<canvas />')[0];
		if(this.canvas)
		{
			this.context = this.canvas.getContext('2d');
			if(this.context)
			{
				
			}
		}
	},
	width: function(value)
	{
		if(!value)
		{
			return this.context.canvas.width;
		}
		this.context.canvas.width = value;
	},
	height: function(value)
	{
		if(!value)
		{
			return this.context.canvas.height;
		}
		this.context.canvas.height = value;
	},
	clipRect: function(x, y, w, h)
	{
		var spr = Sprite.fromSize(w, h);
		spr.context.drawImage(this.canvas, x, y, w, h, 0, 0, w, h);
		return spr;
	}
});